const { apiLogin } = require('../../utils/api')

Page({
  data: {
    phone: '',
    code: '',
    canLogin: false
  },
  onPhone(e){
    this.setData({ phone: e.detail.value }, this.updateCanLogin)
  },
  onCode(e){
    this.setData({ code: e.detail.value }, this.updateCanLogin)
  },
  updateCanLogin(){
    const { phone, code } = this.data
    this.setData({ canLogin: /^1\d{10}$/.test(phone) && /\d{4,6}/.test(code) })
  },
  onLogin(){
    apiLogin(this.data.phone).then(res=>{
      wx.setStorageSync('token', res.token || 'mock-token')
      wx.reLaunch({ url: '/pages/index/index' })
    }).catch(()=>{
      wx.showToast({ title: '登录失败', icon: 'none' })
    })
  }
})






